package com.xingyun.service.student.impl;

import com.xingyun.dao.student.StudentMapper;
import com.xingyun.pojo.Student;
import com.xingyun.service.student.StudentService;
import com.xingyun.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class StudentServiceImpl implements StudentService {
    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<Student> findAll() {
        SqlSession sqlSession = null;
        List<Student> list = null;
        try {
            sqlSession = MyBatisUtils.createSqlSession();
            list = sqlSession.getMapper(StudentMapper.class).findAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtils.closeSqlSession(sqlSession);
        }
        return list;
    }

    /**
     * 根据姓名年级编号  模糊查询
     *
     * @param name
     * @param gradeId
     * @return
     */
    @Override
    public List<Student> findByNameAndGradeId(String name, String gradeId) {
        SqlSession sqlSession = null;
        List<Student> list = null;
        try {
            sqlSession = MyBatisUtils.createSqlSession();
            list = sqlSession.getMapper(StudentMapper.class).findByNameAndGradeId(name, gradeId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtils.closeSqlSession(sqlSession);
        }
        return list;
    }

    /**
     * 根据学生编号  删除
     *
     * @param id
     * @return
     */
    @Override
    public int delById(Integer id) {
        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MyBatisUtils.createSqlSession();
            count = sqlSession.getMapper(StudentMapper.class).delById(id);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtils.closeSqlSession(sqlSession);
        }
        return count;
    }

    /**
     * 根据学生编号 查询单个
     *
     * @param id
     * @return
     */
    @Override
    public Student findOne(Integer id) {
        SqlSession sqlSession = null;
        Student student = null;
        try {
            sqlSession = MyBatisUtils.createSqlSession();
            student = sqlSession.getMapper(StudentMapper.class).findOne(id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtils.closeSqlSession(sqlSession);
        }
        return student;
    }

    /**
     * 添加
     *
     * @param student
     * @return
     */
    @Override
    public int save(Student student) {
        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MyBatisUtils.createSqlSession();
            count = sqlSession.getMapper(StudentMapper.class).save(student);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtils.closeSqlSession(sqlSession);
        }
        return count;
    }

    /**
     * 修改
     *
     * @param student
     * @return
     */
    @Override
    public int upd(Student student) {
        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MyBatisUtils.createSqlSession();
            count = sqlSession.getMapper(StudentMapper.class).upd(student);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtils.closeSqlSession(sqlSession);
        }
        return count;
    }
}
